package com.moyou.weichat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.moyou.weichat.mvp.BasePresenter;
import com.moyou.weichat.mvp.view.BaseView;
import com.moyou.weichat.utils.T;
import com.moyou.weichat.view.LoadDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;

/**
 * sj ：2017年10月16日11:03:11
 * name: ts
 * 用途:封装的BaseActivity
 */
public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements BaseView {
    protected T mPresenter;
    protected Activity mContext;

    public enum TransitionMode {
        LEFT, RIGHT, TOP, BOTTOM, SCALE, FADE
    }

    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        if (toggleOverridePendingTransition()) {
//            switch (getOverridePendingTransitionMode()) {
//                case LEFT:
//                    overridePendingTransition(R.anim.left_in, R.anim.left_out);
//                    break;
//                case RIGHT:
//                    overridePendingTransition(R.anim.right_in, R.anim.right_out);
//                    break;
//                case TOP:
//                    overridePendingTransition(R.anim.top_in, R.anim.top_out);
//                    break;
//                case BOTTOM:
//                    overridePendingTransition(R.anim.bottom_in, R.anim.bottom_out);
//                    break;
//                case SCALE:
//                    overridePendingTransition(R.anim.scale_in, R.anim.scale_out);
//                    break;
//                case FADE:
//                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
//                    break;
//            }
//        }

        super.onCreate(savedInstanceState);
        setContentView(getLayout());

        ButterKnife.bind(this);
        mContext = this;
        createPresenter();
//        if (mPresenter != null)
//            mPresenter.attachView(this);
//        App.getInstance().addActivity(this);
        initTitle();
        initEventAndData();
        initListener();
    }

    @Override
    protected void onStart() {
        super.onStart();

    }
    //显示dialog 加载中状态
    public  void dialogShow(){
        LoadDialog.show(mContext);
    }

    //销毁dialog 加载中状态
    public  void dialogDiss(){
        LoadDialog.dismiss(mContext);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
    }

    protected void setToolBar(Toolbar toolbar, String title) {
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    /**
     * 跳转的Activity
     * @param cls ---要跳转的activituy的class
     */
    public void startAct(Class cls){
        Intent intent = new Intent(mContext  , cls);
        startActivity(intent);
    }

    /**
     * 跳转的Activity
     * @param cls
     */
    public void startActAndBundle(Class cls, Bundle bundle){
        Intent intent = new Intent(mContext  , cls);
        intent.putExtra("bundle",bundle);
        startActivity(intent);
    }

    /**
     * 跳转的Activity
     * @param cls ---要跳转的activituy的class
     * @param requestCode  跳转的请求码
     */
    public void startAct(Class cls,int requestCode){
        Intent intent = new Intent(mContext  , cls);
        startActivityForResult(intent,requestCode);
    }

    @Override
    public void showToast(String content) {
        com.moyou.weichat.utils.T.Tips(content,mContext);
    }

    protected abstract int getLayout();

    protected abstract void initTitle();

    protected abstract void initListener();

    protected abstract void initEventAndData();

    protected abstract boolean toggleOverridePendingTransition();

    protected abstract TransitionMode getOverridePendingTransitionMode();

    protected abstract void  createPresenter();
}
